/*
 * $Id: JsonRpcOperation.java 28642 2006-10-25 13:41:54Z jdamick $
 *
 * Copyright 2006- Revolution Health Group.  All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Revolution Health Group.  (Confidential Information).
 * You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license 
 * agreement you entered into with Revolution Health Group.
 *
 */
package org.javason.jsonrpc.impl;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.javason.jsonrpc.JsonRpcMessageContext;

public class JsonRpcOperation {
	private int 	_numberOfParameters = 0;
	private int 	_numberOfRestParameters = 0;
	private boolean _needsContext = false;
	private Method 	_method;
	private Class[] _params;
	
	public JsonRpcOperation() { }
	
	public JsonRpcOperation(Method method) { 
		initalize(method);
	}
	
	/**
	 * @return Returns the _method.
	 */
	public Method getMethod() {
		return _method;
	}
	
	/**
	 * @return Returns the _needsContext.
	 */
	public boolean isNeedsContext() {
		return _needsContext;
	}
	
	/**
	 * @return Returns the _numberOfParameters.
	 */
	public int getNumberOfParameters() {
		return _numberOfParameters;
	}
	
	/**
	 * @return Returns the _numberOfRestParameters.
	 */
	public int getNumberOfJsonRpcParameters() {
		return _numberOfRestParameters;
	}
	
	public boolean isParameterMatch(Class[] params) {
		boolean result = true;
		
		int index = 0;
		for (Class param : _params) {
			if (!param.equals(params[index])) {
				result = false;
				break;
			}
			index++;
		}
		
		return result;
	}
	
	public List<Class> getParameterTypes() {
		return Arrays.asList(_params);
	}
	
	private void initalize(Method method) {
		_method = method;
		Class params[] = method.getParameterTypes();
		
		_numberOfParameters = params.length;
		_needsContext = false;
		ArrayList<Class> tempParams = new ArrayList<Class>();
		for (Class param : params) {
			if (param.equals(JsonRpcMessageContext.class)) {
				_needsContext = true;
			} else {
				_numberOfRestParameters++;
				tempParams.add(param);
			}
		}
		
		_params = tempParams.toArray(new Class[0]);
	}
}
